package com.jch.demo.cd.config;

import com.jch.demo.cd.controller.AdminController;
import com.jch.demo.cd.controller.IndexController;
import com.jch.demo.cd.controller.LoginController;
import com.jch.demo.cd.interceptor.CrossInterceptor;
import com.jfinal.config.*;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.template.Engine;

/**
 * jFinal 跨域请求处理demo 配置类
 *
 * @author: 迩道
 * @create: 2019/09/26 20:34
 **/
public class CrossDomainConfig extends JFinalConfig {

  static Prop p;

  @Override
  public void configConstant(Constants constants) {
    loadConfig();
    constants.setDevMode(p.getBoolean("devMode", false));
    constants.setInjectDependency(true);
    constants.setInjectSuperClass(true);
  }

  static void loadConfig() {
    if (p == null) {
      //读取文件内容 依次检查文件是否存在 若存在则读取该文件 之后的文件不再检查执行读取
      p = PropKit.useFirstFound("cross-domain-db.properties", "cross-domain-db-dev.properties");
    }
  }

  @Override
  public void configRoute(Routes routes) {
    routes.add("/", IndexController.class);
    routes.add("/login", LoginController.class);
    routes.add("/admin", AdminController.class);
  }


  @Override
  public void configEngine(Engine engine) {

  }


  @Override
  public void configPlugin(Plugins plugins) {

  }


  @Override
  public void configInterceptor(Interceptors interceptors) {
    //将跨域拦截器声明为全局拦截器
    interceptors.add(new CrossInterceptor());
  }


  @Override
  public void configHandler(Handlers handlers) {

  }


}
